<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计划编号" prop="planId">
        <el-input
          v-model="queryParams.planId"
          placeholder="请输入计划编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="BOM单编号" prop="bomId">
        <el-input
          v-model="queryParams.bomId"
          placeholder="请输入BOM单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产日期" style="width: 308px">
        <el-date-picker
          v-model="daterangeProductDate"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="物料编号" prop="materialId">
        <el-input
          v-model="queryParams.materialId"
          placeholder="请输入物料编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工艺编号" prop="processId">
        <el-input
          v-model="queryParams.processId"
          placeholder="请输入工艺编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产线编号" prop="productLineId">
        <el-input
          v-model="queryParams.productLineId"
          placeholder="请输入生产线编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工艺名称" prop="processName">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入工艺名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产线名称" prop="productLineName">
        <el-input
          v-model="queryParams.productLineName"
          placeholder="请输入生产线名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['carbonReport:productPowerReport:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['carbonReport:productPowerReport:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['carbonReport:productPowerReport:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['carbonReport:productPowerReport:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productPowerReportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="计划编号" align="center" prop="planId" />
      <el-table-column label="BOM单编号" align="center" prop="bomId" />
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="生产日期" align="center" prop="productDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.productDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="物料编号" align="center" prop="materialId" />
      <el-table-column label="待产数量" align="center" prop="requireQuantity" />
      <el-table-column label="已产数量" align="center" prop="productQuantity" />
      <el-table-column label="工艺编号" align="center" prop="processId" />
      <el-table-column label="生产线编号" align="center" prop="productLineId" />
      <el-table-column label="耗电量" align="center" prop="powerConsume" />
      <el-table-column label="工艺名称" align="center" prop="processName" />
      <el-table-column label="总耗电量" align="center" prop="totalPowerConsume" />
      <el-table-column label="生产线名称" align="center" prop="productLineName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['carbonReport:productPowerReport:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['carbonReport:productPowerReport:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改VIEW对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="productPowerReportRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="计划编号" prop="planId">
          <el-input v-model="form.planId" placeholder="请输入计划编号" />
        </el-form-item>
        <el-form-item label="BOM单编号" prop="bomId">
          <el-input v-model="form.bomId" placeholder="请输入BOM单编号" />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="生产日期" prop="productDate">
          <el-date-picker clearable
            v-model="form.productDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="物料编号" prop="materialId">
          <el-input v-model="form.materialId" placeholder="请输入物料编号" />
        </el-form-item>
        <el-form-item label="待产数量" prop="requireQuantity">
          <el-input v-model="form.requireQuantity" placeholder="请输入待产数量" />
        </el-form-item>
        <el-form-item label="已产数量" prop="productQuantity">
          <el-input v-model="form.productQuantity" placeholder="请输入已产数量" />
        </el-form-item>
        <el-form-item label="工艺编号" prop="processId">
          <el-input v-model="form.processId" placeholder="请输入工艺编号" />
        </el-form-item>
        <el-form-item label="生产线编号" prop="productLineId">
          <el-input v-model="form.productLineId" placeholder="请输入生产线编号" />
        </el-form-item>
        <el-form-item label="耗电量" prop="powerConsume">
          <el-input v-model="form.powerConsume" placeholder="请输入耗电量" />
        </el-form-item>
        <el-form-item label="工艺名称" prop="processName">
          <el-input v-model="form.processName" placeholder="请输入工艺名称" />
        </el-form-item>
        <el-form-item label="总耗电量" prop="totalPowerConsume">
          <el-input v-model="form.totalPowerConsume" placeholder="请输入总耗电量" />
        </el-form-item>
        <el-form-item label="生产线名称" prop="productLineName">
          <el-input v-model="form.productLineName" placeholder="请输入生产线名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ProductPowerReport">
import { listProductPowerReport, getProductPowerReport, delProductPowerReport, addProductPowerReport, updateProductPowerReport } from "@/api/carbonReport/productPowerReport";

const { proxy } = getCurrentInstance();

const productPowerReportList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeProductDate = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    planId: null,
    bomId: null,
    orderId: null,
    productDate: null,
    materialId: null,
    processId: null,
    productLineId: null,
    processName: null,
    productLineName: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询VIEW列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeProductDate && '' != daterangeProductDate) {
    queryParams.value.params["beginProductDate"] = daterangeProductDate.value[0];
    queryParams.value.params["endProductDate"] = daterangeProductDate.value[1];
  }
  listProductPowerReport(queryParams.value).then(response => {
    productPowerReportList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    planId: null,
    bomId: null,
    orderId: null,
    productDate: null,
    materialId: null,
    requireQuantity: null,
    productQuantity: null,
    processId: null,
    productLineId: null,
    powerConsume: null,
    processName: null,
    totalPowerConsume: null,
    productLineName: null
  };
  proxy.resetForm("productPowerReportRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeProductDate.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.planId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加VIEW";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _planId = row.planId || ids.value
  getProductPowerReport(_planId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改VIEW";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["productPowerReportRef"].validate(valid => {
    if (valid) {
      if (form.value.planId != null) {
        updateProductPowerReport(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addProductPowerReport(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _planIds = row.planId || ids.value;
  proxy.$modal.confirm('是否确认删除VIEW编号为"' + _planIds + '"的数据项？').then(function() {
    return delProductPowerReport(_planIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('carbonReport/productPowerReport/export', {
    ...queryParams.value
  }, `productPowerReport_${new Date().getTime()}.xlsx`)
}

getList();
</script>
